package com.newtouch.bxzs.enterprise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.newtouch.bxzs.common.base.BaseErrorEnum;
import com.newtouch.bxzs.common.base.BaseException;
import com.newtouch.bxzs.common.base.model.UserCustomerCooperationCompanyUserMap;
import com.newtouch.bxzs.common.base.util.UserDetailUtil;
import com.newtouch.bxzs.enterprise.mapper.UserRelationCustomerMapper;
import com.newtouch.bxzs.enterprise.model.vo.UserRelationShipVO;
import com.newtouch.bxzs.enterprise.service.RelationShipService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 三方关系serviceImpl
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/1/14 16:15</td>
 * <td>Amour</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author Amour
 * @version 1.0.0
 * @since 1.0.0
 */
@Service
public class RelationShipServiceImpl implements RelationShipService {

    @Resource
    private UserRelationCustomerMapper userRelationCustomerMapper;

    @Override
    public boolean isExistRelationShip(UserRelationShipVO userRelationShipVO) {
        this.checkParam(userRelationShipVO);
        UserCustomerCooperationCompanyUserMap userRelation = new UserCustomerCooperationCompanyUserMap();
        BeanUtils.copyProperties(userRelationShipVO,userRelation);
        userRelation.setUserInfomation(UserDetailUtil.getUserDetail().getUserJobNumber(),
                UserDetailUtil.getUserDetail().getUserName(),
                UserDetailUtil.getUserDetail().getUserCertificateType(),
                UserDetailUtil.getUserDetail().getUserCertificateNumber());
        userRelation.setCreateInfomation(new Date(),UserDetailUtil.getUserDetail().getUserId(),true);
        QueryWrapper<UserCustomerCooperationCompanyUserMap>queryWrapper = new QueryWrapper<>();
        if (StringUtils.isEmpty(userRelationShipVO.getUnifiedSocialCreditCode())){
            //个人客户判断三方关系，根据三要素
            queryWrapper.eq("CUSTOMER_NAME",userRelation.getCustomerName());
            queryWrapper.eq("CUSTOMER_CERTIFICATE_TYPE",userRelation.getCustomerCertificateType());
            queryWrapper.eq("CUSTOMER_CERTIFICATE_NUMBER",userRelation.getCustomerCertificateNumber());
        }else {
            //企业客户判断三方关系根据客户统一社会信用代码
            queryWrapper.eq("UNIFIED_SOCIAL_CREDIT_CODE",userRelation.getUnifiedSocialCreditCode());
        }
        queryWrapper.eq("USER_JOB_NUMBER",userRelation.getUserJobNumber());
        queryWrapper.eq("USER_NAME",userRelation.getUserName());
        queryWrapper.eq("USER_CERTIFICATE_TYPE",userRelation.getUserCertificateType());
        queryWrapper.eq("USER_CERTIFICATE_NUMBER",userRelation.getUserCertificateNumber());
        queryWrapper.eq("COOPERATION_COMPANY_USER_JOB_NUMBER",userRelation.getCooperationCompanyUserJobNumber());
        queryWrapper.eq("COOPERATION_COMPANY_USER_NAME",userRelation.getCooperationCompanyUserName());
        queryWrapper.eq("COOPERATION_COMPANY_USER_CERTIFICATE_TYPE",userRelation.getCooperationCompanyUserCertificateType());
        queryWrapper.eq("COOPERATION_COMPANY_USER_CERTIFICATE_NUMBER",userRelation.getCooperationCompanyUserCertificateNumber());
        if (userRelationCustomerMapper.selectCount(queryWrapper) <= 0){
            userRelationCustomerMapper.insert(userRelation);
            return false;
        }else{
            return true;
        }
    }

    @Override
    public boolean saveRelationShip(UserRelationShipVO userRelationShipVO) {
        this.checkParam(userRelationShipVO);
        UserCustomerCooperationCompanyUserMap userCustomerCooperationCompanyUserMap = new UserCustomerCooperationCompanyUserMap();
        BeanUtils.copyProperties(userRelationShipVO,userCustomerCooperationCompanyUserMap);
        userCustomerCooperationCompanyUserMap.setCreateInfomation(new Date(),"",true);
        int insertCount = userRelationCustomerMapper.insert(userCustomerCooperationCompanyUserMap);
        if (insertCount <= 0){
            return false;
        }
        return true;
    }

    public void checkParam(UserRelationShipVO userRelationShip){
        //判断对端用户信息是否为空
        if ( StringUtils.isEmpty(userRelationShip.getCooperationCompanyUserJobNumber())
                || StringUtils.isEmpty(userRelationShip.getCooperationCompanyUserName())
                || StringUtils.isEmpty(userRelationShip.getCooperationCompanyUserCertificateType())
                || StringUtils.isEmpty(userRelationShip.getCooperationCompanyUserCertificateNumber())
        ) {
            throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
        }

        if (StringUtils.isEmpty(userRelationShip.getUnifiedSocialCreditCode())){
            //个人客户信息判断
            if (StringUtils.isEmpty(userRelationShip.getCustomerName())
                    || StringUtils.isEmpty(userRelationShip.getCustomerCertificateType())
                    || StringUtils.isEmpty(userRelationShip.getCustomerCertificateNumber())
                    || StringUtils.isEmpty(userRelationShip.getCustomerSex())
                    || StringUtils.isEmpty(userRelationShip.getCustomerPhone())
                    || StringUtils.isEmpty(userRelationShip.getCooperationCompanyUserJobNumber())
                    || StringUtils.isEmpty(userRelationShip.getCooperationCompanyUserName())
                    || StringUtils.isEmpty(userRelationShip.getCooperationCompanyUserCertificateType())
                    || StringUtils.isEmpty(userRelationShip.getCooperationCompanyUserCertificateNumber())
            ) {
                throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
            }
        }
    }
}
